package com.pickgliss.ui.controls
{
	import com.pickgliss.ui.ComponentFactory;
	import com.pickgliss.ui.core.Component;
	import com.pickgliss.ui.image.Image;
	import com.pickgliss.utils.DisplayUtils;
	import com.pickgliss.utils.ObjectUtils;
	import com.pickgliss.utils.PNGHitAreaFactory;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.InteractiveObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	[Event(name="select",type="flash.events.Event")]
	public class SelectedButton extends BaseButton implements ISelectable
	{
		public static const P_selectedStyle:String = "selectedStyle";
		public static const P_unSelectedStyle:String = "unSelectedStyle";

		public function SelectedButton()
		{
			super();
		}
		
		protected var _selected:Boolean;
		protected var _selectedButton:DisplayObject;
		protected var _selectedStyle:String;
		protected var _unSelectedButton:DisplayObject;
		protected var _unSelectedStyle:String;
		private var _autoSelect:Boolean = true;
		private var _selectHitArea:Sprite;
		private var _unSelectHitArea:Sprite;

		public function set autoSelect(value:Boolean):void
		{
			if(_autoSelect == value)return ;
			_autoSelect = value;
		}
		
		override public function dispose():void
		{
			removeEvent();
			ObjectUtils.disposeObject(_selectedButton);
			_selectedButton = null;
			ObjectUtils.disposeObject(_unSelectedButton);
			_unSelectedButton = null;
			ObjectUtils.disposeObject(_selectHitArea);
			ObjectUtils.disposeObject(_unSelectHitArea);
			_selectHitArea = null;
			_unSelectHitArea = null;
			super.dispose();
		}
		
		public function get selected():Boolean
		{
			return _selected;
		}
		
		public function set selected(value:Boolean):void
		{
			_selected = value;
			if(_selectedButton)_selectedButton.visible = _selected;
			if(_unSelectedButton)_unSelectedButton.visible = !_selected;
			dispatchEvent(new Event(Event.SELECT));
			drawHitArea();
		}
		/**
		 * 
		 * 选中时的按钮样式
		 * 通过ComponentFactory创建
		 * 
		 */		
		public function get selectedStyle():String
		{
			return _selectedStyle;
		}
		
		public function set selectedStyle(stylename:String):void
		{
			if(_selectedStyle == stylename) return;
			_selectedStyle = stylename;
			onPropertiesChanged(P_selectedStyle);
		}
		
		override public function setFrame(frameIndex:int):void
		{
			super.setFrame(frameIndex);
			if(_selectedButton)DisplayUtils.setFrame(_selectedButton,_currentFrameIndex);
			if(_unSelectedButton)DisplayUtils.setFrame(_unSelectedButton,_currentFrameIndex);
		}
		/**
		 * 
		 * @param button 设置选中时的按钮
		 * 通过ComponentFactory创建
		 * 
		 */		
		public function setSelectedButton(button:DisplayObject):void
		{
			ObjectUtils.disposeObject(_selectedButton);
			_selectedButton = button;
			DisplayUtils.setDisplayObjectNotEnable(_selectedButton);
			setFrame(1);
		}
		/**
		 * 
		 * @param button 设置未选中时的按钮
		 * 
		 */		
		public function setUnselectedButton(button:DisplayObject):void
		{
			ObjectUtils.disposeObject(_unSelectedButton);
			_unSelectedButton = button;
			DisplayUtils.setDisplayObjectNotEnable(_unSelectedButton);
			setFrame(1);
		}
		/**
		 * 
		 * @return 未选中时的按钮样式
		 * 
		 */		
		public function get unSelectedStyle():String
		{
			return _unSelectedStyle;
		}
		
		public function set unSelectedStyle(stylename:String):void
		{
			if(_unSelectedStyle == stylename) return;
			_unSelectedStyle = stylename;
			onPropertiesChanged(P_unSelectedStyle);
		}
		override protected function __onMouseClick(event:MouseEvent):void
		{
			super.__onMouseClick(event);
			if(_autoSelect)selected = !_selected;
		}
		
		override protected function adaptHitArea():void
		{
			_PNGHitArea.alpha = 0;
			_PNGHitArea.x = _selected?_selectedButton.x : _unSelectedButton.x;
			_PNGHitArea.y = _selected?_selectedButton.y : _unSelectedButton.y;
		}
		
		override protected function addChildren():void
		{
			super.addChildren();
			if(_selectedButton)addChild(_selectedButton);
			if(_unSelectedButton)addChild(_unSelectedButton);
		}
		override protected function drawHitArea():void
		{
			if (_PNGHitArea && contains(_PNGHitArea))removeChild(_PNGHitArea);
			if (_transparentEnable)
			{
				if(_selectHitArea == null)_selectHitArea = PNGHitAreaFactory.drawHitArea(DisplayUtils.getDisplayBitmapData(_selectedButton));
				if(_unSelectHitArea == null)_unSelectHitArea = PNGHitAreaFactory.drawHitArea(DisplayUtils.getDisplayBitmapData(_unSelectedButton));
				_PNGHitArea = _selected ? _selectHitArea : _unSelectHitArea;
				adaptHitArea();
				_PNGHitArea.alpha = 0;
				hitArea = _PNGHitArea;
				addChild(_PNGHitArea);
			}else
			{
				if (_PNGHitArea && contains(_PNGHitArea))removeChild(_PNGHitArea);
			}
		}
		
		override protected function onProppertiesUpdate():void
		{
			super.onProppertiesUpdate();
			if(_changedPropeties[P_unSelectedStyle])
			{
				setUnselectedButton(ComponentFactory.Instance.creat(_unSelectedStyle));
				selected = _selected;
				if(_selectedButton)
				{
					_width = _selectedButton.width;
					_height = _selectedButton.height;
				}
			}
			
			if(_changedPropeties[P_selectedStyle])
			{
				setSelectedButton(ComponentFactory.Instance.creat(_selectedStyle));
				selected = _selected;
				if(_unSelectedButton)
				{
					_width = _unSelectedButton.width;
					_height = _unSelectedButton.height;
				}
			}
		}
	}
}